package de.syfy.project.engine;

import static org.lwjgl.opengl.GL11.*;
import de.syfy.project.engine.utils.Vector2D;
import java.io.IOException;

/**
 *
 * @author Timo
 */
public class Particle{
    
    private Texture texture;
    private Vector2D vel,loc,acc;
    private float timer, interval;
    
    public Particle(Vector2D loc, Vector2D vel, Vector2D acc, 
                            TextureLoader loader, String texturePath, 
                            float duration, float interval){
        this(loader,texturePath);
        this.loc = loc;
        this.vel = vel;
        this.acc = acc;
        this.timer = duration;
        this.interval = interval;
    }
    
    public Particle(TextureLoader loader,String texturePath){
        try {
            texture = loader.getTexture(texturePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void setTexture(TextureLoader loader, String texturePath){
        try {
            texture = loader.getTexture(texturePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void setLoc(Vector2D loc){
        this.loc = loc;
    }
    
    public void setVel(Vector2D vel){
        this.vel = vel;
    }
    
    public void setAcc(Vector2D acc){
        this.acc = acc;
    }
    
    public void run(){
        update();
        render();
    }
    
    private void update(){
        vel.add(acc);
        loc.add(vel);
        timer -= interval;
    }
    
    private void render(){
        glEnable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
        glPushMatrix();
        texture.bind();
        glTranslatef(loc.getX(), loc.getY(), 0);
        glBegin(GL_QUADS);
            glColor4f(255,255,255,timer);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(0, texture.getHeight());
            glVertex2f(0, texture.getImageHeight());
            glTexCoord2f(texture.getWidth(), texture.getHeight());
            glVertex2f(texture.getImageWidth(), texture.getImageHeight());
            glTexCoord2f(texture.getWidth(), 0);
            glVertex2f(texture.getImageWidth(), 0);
        glEnd();
        glPopMatrix();
        glDisable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
    }
    
    
    public boolean isDead(){
        return timer <= 0.0;
    }
}
